﻿using Acme.SmartWMS.BasicInfo;
using Acme.SmartWMS.BasicInfo.CommodityDetails;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using System;
using Volo.Abp.Domain.Repositories;
using System.Collections.Generic;
using Acme.SmartWMS.DataBasic;
using IdentityModel.OidcClient;

namespace Acme.SmartWMS.Web.BasicInfo.CommodityDetails
{
    public class CommodityDetailController : Controller
    {

        private readonly ICommodityDetailAppService _commodityDetailRepository;

        public CommodityDetailController(ICommodityDetailAppService commodityDetailRepository)
        {
            _commodityDetailRepository = commodityDetailRepository;
        }

        public IActionResult Add()
        {
            return View();
        }
        public IActionResult Edit()
        {
            return View();
        }

        public IActionResult CommodityDetailList()
        {
            return View();
        }

        [HttpPost]
        public async Task<int> AddCommodityDetail(CommodityDetailDTO dto)
        {
            var result =  await _commodityDetailRepository.AddCommodityDetail(dto);
            return result;
        }

        [HttpPost]
        public async Task<int> DeleteCommodityDetail(int id)
        {
            var result = await _commodityDetailRepository.DeleteCommodityDetail(id);
            return result;
        }

        [HttpGet]
        public JsonResult GetCommodityDetailById(int id)
        {
            var result =  _commodityDetailRepository.GetCommodityDetailById(id);
            return Json(new { data = result, code = "0", msg = "" });
        }

        [HttpGet]
        public JsonResult GetCommodityDetails(string? name, int page = 1, int limit = 2)
        {
            var result = _commodityDetailRepository.GetCommodityDetails(name, page, limit);
            return Json(new { data = result.Queryable, count = result.RowCount, code = "0", msg = "" });
        }

        [HttpPost]
        public async Task<int> UpdateCommodityDetail(CommodityDetailDTO dto)
        {
            var result = await _commodityDetailRepository.UpdateCommodityDetail(dto);
            return result;
        }

        [HttpGet]
        public List<DictItem> GetDictItemById(int id=1)
        {
            var list = _commodityDetailRepository.GetDictItemById(id);
            return list;
        }

        [HttpPost]
        public async Task<int> DeleAll(string id)
        {
            int res = 0;
            foreach (var ids in id.Split(","))
            {
                res = await _commodityDetailRepository.DeleteCommodityDetail(int.Parse(ids));
            }
            return res;
        }
    }
}
